package test3_21.demo02;

import java.lang.reflect.Array;
import java.util.Arrays;

public class MyAdd {

    private int[] arr;

    //追加一个值
    public void add(int value) {
        int[] arr1 = new int[arr.length+1];
        for (int i = 0; i < arr.length; i++) {
            arr1[i] = arr[i];
        }
        arr1[arr1.length-1] = value;
        this.arr = arr1;
        System.out.println(Arrays.toString(arr));
    }

    //根据索引，删除一个值
    void remove(int index){
        int[] arr1 = new int[arr.length-1];
        for (int i = 0; i < arr1.length; i++) {
            if(i<index){
                arr1[i] = arr[i];
            }else {
                arr1[i] = arr[i+1];
            }
        }
        this.arr = arr1;
        System.out.println(Arrays.toString(arr));
    }

    //在指定位置插入一个数值
    void add(int position,int value){
        int[] arr1 = new int[arr.length+1];
        for (int i = 0; i < arr1.length; i++) {
            if(i<position){
                arr1[i] = arr[i];
            }else if(i == position){
                arr1[i] = value;
            }else {
                arr1[i] = arr[i-1];
            }
        }
        this.arr = arr1;
        System.out.println(Arrays.toString(arr));
    }

    //修改某个位置的数值
    public void set(int position,int value){
        int[] arr1 = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            if(i == position){
                arr1[i] = value;
            }else {
                arr1[i] = arr[i];
            }
        }
        this.arr = arr1;
        System.out.println(Arrays.toString(arr));
    }


    //根据索引，获得元数的值
    public int get(int index) {
        int n = 0;
        for (int i = 0; i < arr.length; i++) {
            if (i == index) {
                n = arr[i];
                break;
            }
        }
        return n;
    }


    //获得动态数组中元素的个数
    public int size(){
        return arr.length;
    }


    public MyAdd(int[] arr) {
        this.arr = arr;
    }

}
